import QtQuick
import QtQuick.Controls
import QtQuick.Layouts
//import QtQuick.Controls.Styles

import Shared

/*基础上GamePage.qml，所以有errorMessage,infoMessage。
  有1个title，1个ListView和2个GameButton。
*/
GamePage {
    /*QQ会重载吗？
    */
    function init()
    {
        console.log("show ParaPlc.qml");
        row_info.vis= false;
    }


    Text {
        id: txt_title
        anchors.top: parent.top
        anchors.horizontalCenter: parent.horizontalCenter

        text: qsTr("PLC通信参数")
        color: "green"


        font.pointSize: 48
    }

    //通信地址
    RowLayout{
        id: row1
        anchors.top: txt_title.bottom

        Text {
            text: qsTr("通信地址")
            color: "green"
        }

        TextField{
            id: txtfd_addr

            text: deviceHandler.plc_addr                //测试ok
            //text: deviceHandler.plc_addr.toString()   //测试ok
            color: "white"
        }

        Button{
            id: btn_addr_rd
            text: qsTr("读取")
            //color: pressed?red:green

            onClicked: {
                console.log("read plc addr");
                row_info.vis= true;
                //void data03h_to_periph(uint16_t addr, uint32_t cnt);
                deviceHandler.data03h_to_periph(GameSettings.addr_com_plc_addr, 2); //参数2固定为2。
            }
        }

        Button{
            id: btn_addr_wr
            text: qsTr("写入")
            //color: pressed?red:green

            onClicked: {
                console.log("write plc addr");
                row_info.vis= true;
                deviceHandler.data10h_to_periph(GameSettings.addr_com_plc_addr, Number(txtfd_addr.text));
            }
        }
    }


    //通信协议
    RowLayout{
        id: row_protocol
        anchors.top: row1.bottom

        Text {
            text: qsTr("通信协议")
            color: "green"
        }

//        TextField{
//            id: txtfd_baud
//            text: "0"
//        }
        ComboBox{
            id:combobox_protocol
            model: ["rtu","Cont"]
            currentIndex: deviceHandler.plc_protocol_idx
        }

        Button{
            id: btn_protocol_rd
            text: qsTr("读取")
            //color: pressed?red:green

            onClicked: {
                console.log("read plc protocol");
                row_info.vis= true;
                deviceHandler.data03h_to_periph(GameSettings.addr_com_plc_mode_idx, 0);
            }
        }

        Button{
            id: btn_protocol_wr
            text: qsTr("写入")
            //color: pressed?red:green

            onClicked: {
                console.log("write plc protocol");
                row_info.vis= true;
                deviceHandler.data10h_to_periph(GameSettings.addr_com_plc_mode_idx, combobox_protocol.currentIndex);
            }
        }
    }


    //波特率
    RowLayout{
        id: row2
        anchors.top: row_protocol.bottom

        Text {
            text: qsTr("波特率　")
            color: "green"
        }

//        TextField{
//            id: txtfd_baud
//            text: "0"
//        }
        ComboBox{
            id:combobox_baud
            model: ["9600", "19200","38400", "57600", "115200"]
            currentIndex: deviceHandler.plc_baud_idx
        }

        Button{
            id: btn_baud_rd
            text: qsTr("读取")
            //color: pressed?red:green

            onClicked: {
                console.log("read plc baud");
                row_info.vis= true;
                deviceHandler.data03h_to_periph(GameSettings.addr_com_plc_baud_idx, 0);
            }
        }

        Button{
            id: btn_baud_wr
            text: qsTr("写入")
            //color: pressed?red:green

            onClicked: {
                console.log("write plc baud");
                row_info.vis= true;
                deviceHandler.data10h_to_periph(GameSettings.addr_com_plc_baud_idx, combobox_baud.currentIndex);
            }
        }
    }


    //格式
    RowLayout{
        id: row3
        anchors.top: row2.bottom

        Text {
            text: qsTr("通信格式")
            color: "green"
        }

//        TextField{
//            id: txtfd_fmt
//            text: "0"
//        }

        ComboBox{
            id:combobox_fmt
            model: ["8-E-1", "8-O-1","8-n-1", "8-n-2"]
            currentIndex: deviceHandler.plc_fmt_idx
        }

        Button{
            id: btn_fmt_rd
            text: qsTr("读取")
            //color: pressed?red:green

            onClicked: {
                console.log("read plc fmt");
                row_info.vis= true;
                deviceHandler.data03h_to_periph(GameSettings.addr_com_plc_format_idx, 0);
            }
        }

        Button{
            id: btn_fmt_wr
            text: qsTr("写入")
            //color: pressed?red:green

            onClicked: {
                console.log("write plc fmt");
                row_info.vis= true;
                deviceHandler.data10h_to_periph(GameSettings.addr_com_plc_format_idx, combobox_fmt.currentIndex);
            }
        }
    }


    //响应延时（发送间隔）
    RowLayout{
        id: row_echo_delay
        anchors.top: row3.bottom

        Text {
            text: qsTr("发送间隔")
            color: "green"
        }

//        TextField{
//            id: txtfd_delay
//            text: "0"
//        }
        ComboBox{
            id:combobox_interval
            model: ["10","20", "30", "40", "50", "100", "200"]
            currentIndex: deviceHandler.plc_interval_idx
        }

        Button{
            id: btn_interval_rd
            text: qsTr("读取")
            //color: pressed?red:green

            onClicked: {
                console.log("read plc interval");
                row_info.vis= true;
                deviceHandler.data03h_to_periph(GameSettings.addr_com_interval_idx, 0);
            }
        }

        Button{
            id: btn_interval_wr
            text: qsTr("写入")
            //color: pressed?red:green

            onClicked: {
                console.log("write plc interval");
                row_info.vis= true;
                deviceHandler.data10h_to_periph(GameSettings.addr_com_interval_idx, combobox_interval.currentIndex);
            }
        }
    }


    //提示信息。
    RowLayout{
        property bool vis: true

        id: row_info
        anchors.top: row_echo_delay.bottom
        anchors.horizontalCenter: parent.horizontalCenter
        //Layout.alignment: Qt.AlignHCenter

        Text{
            text:qsTr(" ")      //占位，以免下面的Text{}隐藏时，造成下面的控件位置变化。
        }

        Text{
            id: txt_info_plc
            anchors.centerIn: parent.Center
            visible: deviceHandler.txt_ok_v && parent.vis

            //text: qsTr("成功")
            text: deviceHandler.txt_info
            color: deviceHandler.txt_color      //"green"
        }
    }

    //帮助信息
    Text {
        //id: txt_title
        anchors.bottom: parent.bottom
        anchors.bottomMargin: 20
        anchors.horizontalCenter: parent.horizontalCenter

        text: qsTr("须依次点击读取，写入")
        color: "green"


        font.pointSize: 24
    }


    Timer{
        id: timer_info_plc
        interval: 500
        running: deviceHandler.txt_ok_v && row_info.vis

        onTriggered: {
            console.log("timer tmo")
            row_info.vis= false
            //row_info.txt_info_set.visible= false;   //和绑定属性deviceHandler.txt_ok_v不同步了。
        }
    }
}
